package com.soushin.chinachess.widget;

import java.util.List;

import com.soushin.chinachess.R;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.TextView;
/**
 * 设置-选择弹出框
 * @author Administrator
 *
 */
public class ListDialog extends Dialog implements android.view.View.OnClickListener {

	private TextView tvItem1, tvItem2, tvItem3, tvItem4,tvItem5,tvCancel;
	private List<String> itemList;
	private OnSelectListener onSelectListener;
	public ListDialog(Context context, List<String> itemList) {
		super(context, R.style.myDialog);
		setContentView(R.layout.dialog_list);
		// TODO Auto-generated constructor stub
		this.itemList = itemList;
		initData(context);
	}

	private void initData(Context context) {
		// TODO Auto-generated method stub
		tvItem1 = (TextView) findViewById(R.id.tv_item1);
		tvItem2 = (TextView) findViewById(R.id.tv_item2);
		tvItem3 = (TextView) findViewById(R.id.tv_item3);
		tvItem4 = (TextView) findViewById(R.id.tv_item4);
		tvItem5= (TextView) findViewById(R.id.tv_item5);
		tvCancel = (TextView) findViewById(R.id.tv_cancel);

		tvItem1.setOnClickListener(this);
		tvItem2.setOnClickListener(this);
		tvItem3.setOnClickListener(this);
		tvItem4.setOnClickListener(this);
		tvItem5.setOnClickListener(this);
		tvCancel.setOnClickListener(this);
		tvItem1.setText(itemList.get(0));
		tvItem2.setText(itemList.get(1));
		setCanceledOnTouchOutside(false);
		switch (itemList.size()) {
		case 3:
			tvItem3.setVisibility(View.VISIBLE);
			tvItem3.setText(itemList.get(2));
			break;
		case 4:
			tvItem3.setVisibility(View.VISIBLE);
			tvItem4.setVisibility(View.VISIBLE);
			tvItem3.setText(itemList.get(2));
			tvItem4.setText(itemList.get(3));
			break;
		case 5:
			tvItem3.setVisibility(View.VISIBLE);
			tvItem4.setVisibility(View.VISIBLE);
			tvItem5.setVisibility(View.VISIBLE);
			tvItem3.setText(itemList.get(2));
			tvItem4.setText(itemList.get(3));
			tvItem5.setText(itemList.get(4));
			break;	
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		dismiss();
		switch(v.getId()) {
		case R.id.tv_item1:
			if(onSelectListener!=null) {
				onSelectListener.onSelect(0, tvItem1.getText().toString());
			}
			break;
		case R.id.tv_item2:
			if(onSelectListener!=null) {
				onSelectListener.onSelect(1, tvItem2.getText().toString());
			}
			break;
		case R.id.tv_item3:
			if(onSelectListener!=null) {
				onSelectListener.onSelect(2, tvItem3.getText().toString());
			}
			break;
		case R.id.tv_item4:
			if(onSelectListener!=null) {
				onSelectListener.onSelect(3, tvItem4.getText().toString());
			}
			break;
		case R.id.tv_item5:
			if(onSelectListener!=null) {
				onSelectListener.onSelect(4, tvItem5.getText().toString());
			}
			break;
		case R.id.tv_cancel:
			break;
		}
	}
	/**
	 * 选择回调
	 * @author Administrator
	 *
	 */
	public interface OnSelectListener{
		void onSelect(int position,String item);
	}
	
	public void setOnSelectListener(OnSelectListener listener) {
		this.onSelectListener=listener;
	}
	

}
